from django.http import HttpResponse, FileResponse
from django.http import HttpResponse, StreamingHttpResponse
from django.shortcuts import render
import os


def index(request):
    return render(request, 'index.html')


def log2(request):
    return render(request, 'xgboost.html')


def log3(request):
    return render(request, 'dispatch.html')


def bikeline(request):
    # print("POST method")
    # context = request.POST
    # postbody = request.body
    # print(type(postbody))
    # print(postbody)
    return render(request, "bikeline.html")

import pandas as pd
import os
from SharingBikeDispatchBasedOnDjango import settings

#import xgboost
import pandas as pd
import numpy as np
def upload_xgb(request):
    if request.method == "POST":  # 请求方法为POST时，进行处理
        myFile = request.FILES.get("myfile", None)  # 获取上传的文件，如果没有文件，则默认为None
        if not myFile:
            flag = "文件上传失败！"
            return render(request, "xgboost.html", {'flag': flag})
        flag = "上传成功！"
        filedir = os.path.abspath(os.path.join(settings.BASE_DIR, './file/'))
        destination = open(os.path.join(filedir, myFile.name),
                           'wb+')  # 打开特定的文件进行二进制的写操作
        for chunk in myFile.chunks():  # 分块写入文件
            destination.write(chunk)
        destination.close()
        # 调用xgboost算法
        # m = xgboost.Booster(model_file='D:/RegularFile/SharingBikeDispatchBasedOnDjango/algorithm/xgb.model')
        # x = pd.read_csv('D:/RegularFile/SharingBikeDispatchBasedOnDjango/file/test.csv')
        # x = np.array(x.loc[:, ['deposit', 'instrength', 'outstrength', 'indegree', 'outdegree', 'pagerank']])
        # d = xgboost.DMatrix(x)
        # y = m.predict(d)
        # yd = pd.DataFrame(y)
        # yd.to_csv('D:/RegularFile/SharingBikeDispatchBasedOnDjango/result/test_pre.csv')
        return render(request, "xgboost.html", {'flag': flag})


def download_xgb(request):
    file_name = request.POST['filename']
    filedir = os.path.abspath(os.path.join(settings.BASE_DIR, './result/'))
    file = open(filedir+"\\"+file_name, 'rb')
    response = FileResponse(file)

    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment; filename={}'.format(file_name)
    return response



from algorithm import aco


def upload_aco(request):
    if request.method == "POST":  # 请求方法为POST时，进行处理
        myFile = request.FILES.get("myfile", None)  # 获取上传的文件，如果没有文件，则默认为None
        if not myFile:
            flag = "文件上传失败！"
            return render(request, "dispatch.html", {'flag': flag})
        flag = "上传成功！"
        filedir = os.path.abspath(os.path.join(settings.BASE_DIR, './file/'))
        destination = open(os.path.join(filedir, myFile.name),
                           'wb+')  # 打开特定的文件进行二进制的写操作
        for chunk in myFile.chunks():  # 分块写入文件
            destination.write(chunk)
        destination.close()
        # 调用蚁群算法
        res = aco.fun()
        geo = res[0]
        point = res[1]
        f = open('result/path.txt', 'w+')
        for ind, path in zip(point, geo):
            str_ind = str(ind)
            str_path = str(path)
            f.write(str_ind + '\n')
            f.write(str_path + '\n')
        f.close

        return render(request, "dispatch.html", {'flag': flag})


def download_aco(request):
    file_name = request.POST['filename']
    filedir = os.path.abspath(os.path.join(settings.BASE_DIR, './result/'))
    file = open(filedir+"\\"+file_name, 'rb')
    response = FileResponse(file)

    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment; filename={}'.format(file_name)
    return response


def heatmap(request):
    return render(request, 'heatmap.html')


def map_new(request):
    return render(request, 'map_new.html')
